package com.example.Controller;


import com.example.entity.Users;
import com.example.service.impl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Controller
@RequestMapping("/admin")
public class UserCtrl {
    @Resource
    UserServiceImpl userService;

    @GetMapping("selectOne")
    public Users selectOne(Integer id) {
        return this.userService.findById(id);
    }

    @RequestMapping(value = "/users",method = {RequestMethod.GET , RequestMethod.POST})
    public String findAllusers(Model model,@RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",required = false, defaultValue = "4") Integer pageSize){
        PageInfo<Users> usersList = userService.queryAll(pageNum,pageSize);
        model.addAttribute("usersList",usersList);
        return "usersList";
    }

    @GetMapping("/delUsers/{id}")
    public String delUsers(@PathVariable("id") Integer id, Model model){
        int num = userService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/admin/users";
    }

    @GetMapping("/users/{id}")
    public String getUsersById(@PathVariable("id") int id,Model model){
        Users users = userService.findById(id);
        model.addAttribute("users",users);
        return "usersEdit";
    }

    @PostMapping("/editUsers")
    public String updateUsers(Users users,Model model){
        int i = userService.update(users);
        model.addAttribute("msg","编辑成功");
        return "forward:/admin/users";
    }
    @GetMapping("/gotoaddUsers")
    public String gotoAddusers(){
        return "usersAdd";
    }
    @PostMapping("/addUsers")
    public String addUsers(Users users){
        int insert = userService.insert(users);
        return "forward:/admin/users";
    }

}
